//
//  CityPickerView.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/5/16.
//  Copyright © 2018年 Lwx. All rights reserved.
//

#import "CityPickerView.h"
#define PICKER_HEIGHT   216/WIDTH_6_SCALE

@interface CityPickerView()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSInteger _provinceIndex;   // 省份选择 记录
    NSInteger _cityIndex;       // 市选择 记录
    NSInteger _districtIndex;   // 区选择 记录
}
@property (nonatomic, strong)UIPickerView * pickerView;
@property (nonatomic, strong)NSString * completeAddressStr;//完整地址
@property (nonatomic, strong)NSString * provinceStr;
@property (nonatomic, strong)NSString * cityStr;
@property (nonatomic, strong)NSString * sepcificAddressStr;//具体地址
/**
 *  数据源
 */
@property (nonatomic, strong) NSArray * arrayDS;
@property (nonatomic, strong) UIButton * bgBtn;
@end

@implementation CityPickerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height)];
        [btn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:btn];
        self.bgBtn = btn;
        
        [self setUpView];
        [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
        [self.pickerView selectRow:0 inComponent:0 animated:YES];
        
        [self pickerView:self.pickerView didSelectRow:0 inComponent:1];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        
        [self pickerView:self.pickerView didSelectRow:0 inComponent:2];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
    }return self;
}

- (void)setUpView{
    //选择器整体背景
    int btnH = 30/WIDTH_6_SCALE;
    UIView * pickerBGView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - PICKER_HEIGHT - btnH - BOTTOM_HEIGHT, SCREEN_WIDTH, PICKER_HEIGHT + btnH)];
    pickerBGView.backgroundColor = [UIColor whiteColor];
    [self addSubview:pickerBGView];
    //选择器
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, btnH, SCREEN_WIDTH, PICKER_HEIGHT)];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    [pickerBGView addSubview:self.pickerView];
    //选择器上按钮
    int border = 20/WIDTH_6_SCALE;
    UIButton * sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(border, 0, SCREEN_WIDTH/2 - border * 2, btnH)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
    sureBtn.titleLabel.font = DEF_FontSize_16;
    [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [pickerBGView addSubview:sureBtn];
    
    UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(border + SCREEN_WIDTH/2, 0, sureBtn.width, btnH)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = DEF_FontSize_14;
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [pickerBGView addSubview:cancelBtn];
}

-(NSArray *)arrayDS{
    if(!_arrayDS){
        NSString * path = [[NSBundle mainBundle] pathForResource:@"Province" ofType:@"plist"];
        _arrayDS = [[NSArray alloc] initWithContentsOfFile:path];
    }
    return _arrayDS;
}

#pragma mark - pickerViewDelegate
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0){
        return self.arrayDS.count;
    }
    else if (component == 1){
        return [self.arrayDS[_provinceIndex][@"citys"] count];
    }
    else{
        return [self.arrayDS[_provinceIndex][@"citys"][_cityIndex][@"districts"] count];
    }
}

// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(component == 0){
        return self.arrayDS[row][@"province"];
    }
    else if (component == 1){
        return self.arrayDS[_provinceIndex][@"citys"][row][@"city"];
    }
    else{
        return self.arrayDS[_provinceIndex][@"citys"][_cityIndex][@"districts"][row];
    }
}

// 滑动或点击选择，确认pickerView选中结果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component == 0){
        _provinceIndex = row;
        _cityIndex = 0;
        _districtIndex = 0;
        self.provinceStr = self.arrayDS[row][@"province"];
        [self.pickerView reloadComponent:1];
        [self.pickerView reloadComponent:2];
    }
    else if (component == 1){
        _cityIndex = row;
        _districtIndex = 0;
        
        [self.pickerView reloadComponent:2];
    }
    else{
        _districtIndex = row;
    }
// 重置当前选中项
//    [self resetPickerSelectRow];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = getColor(dividerColor);
        }
    }
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.font = DEF_FontSize_16;
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.textColor = [UIColor blackColor];
    if(component == 0){
        genderLabel.text =  self.arrayDS[row][@"province"];
    }
    else if (component == 1){
        genderLabel.text =  self.arrayDS[_provinceIndex][@"citys"][row][@"city"];
        self.cityStr = self.arrayDS[_provinceIndex][@"citys"][row][@"city"];
    }
    else{
        genderLabel.text =  self.arrayDS[_provinceIndex][@"citys"][_cityIndex][@"districts"][row];
        self.sepcificAddressStr = self.arrayDS[_provinceIndex][@"citys"][_cityIndex][@"districts"][row];
    }
    self.completeAddressStr = [NSString stringWithFormat:@"%@ %@ %@", self.provinceStr, self.cityStr, self.sepcificAddressStr];
    return genderLabel;
}

#pragma mark - action
- (void)cancelBtnClick{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showView{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

- (void)sureBtnClick:(UIButton *)sender{
    NSLog(@"%@", self.completeAddressStr);
    if (self.getAddress) {
        self.getAddress(self.completeAddressStr, self.sepcificAddressStr);
        [self cancelBtnClick];
    }
}
@end
